<? 
$pagename=LA_UPLOAD_DOC;
if (!$admin_area)
	include_once("header_inc.php");


if (!$admin_area)
{
	check_valid_user("");
	include_once("member_header.php");	
}
$doc_ad_id=getParamInt("doc_ad_id","");
if ($doc_ad_id)
{
	$result = q("select ad_owner from $ads_tbl where ad_id = $doc_ad_id");
	$row = mysql_fetch_array($result);
	$ad_owner = $row["ad_owner"];
	
	if ($ad_owner <> $_SESSION["valid_user"] AND !$admin_area)
		criticalError(LA_ERROR,LA_MUSTBEOWNER);
}
$tplUpload_docs=new TplLoad;
if (isset($_REQUEST["doc_ad_id"]))
{
	$doc_ad_id = round($_REQUEST["doc_ad_id"]);
	$tplUpload_docs->assign("doc_ad_id",$doc_ad_id);
}
$tplUpload_docs->assign("url", "http://$set_url");
$tplUpload_docs->assign("phpself",$_SERVER['PHP_SELF']);


$size = floor($set_doc_limit*1024);

if (getParam("del_id",""))
{
	deleteDoc(getParamInt("del_id",""));
	q("update $ads_tbl set ad_has_doc=ad_has_doc-1 where ad_id=$doc_ad_id AND ad_has_doc>0");
	
}

if (getParam("submit",""))
{
	$lNumUploaded=getNumberOfDocumentsUploaded($doc_ad_id);
	if ($lNumUploaded>=$set_doc_num)
		$lStop=1;
	
}


if (isset($_REQUEST["submit"]) AND !$lStop)
{
	
	$FileArray=$_FILES["photo"];
	$SaveAs=$set_path . "/images/docs/$doc_ad_id";
	$MaxSize="$piclimit";
	$ad_id=getParamInt("doc_ad_id","");
	$returnValue=uploadImage($FileArray,$SaveAs,$set_doc_limit,$DimensionArray,2,"");
	
	if (is_int($returnValue))
		$tplUpload_docs->assign("error",getErrorMsgVideo($returnValue));
	else 
	{
		foreach ($returnValue as $item)
		{
			//echo "Filen $item ble lastet opp!<br>";
			//echo "<img src='images/$item'><br>";
			@chmod("images/docs/$item",octdec($set_chmod));
			
			
			
		}
		
		$sq = "INSERT INTO $doc_tbl (f_adid,f_file) VALUES ('$doc_ad_id','$item')";
		$result=q($sq);
		$id = mysql_insert_id();
			
		$sq = "update $ads_tbl set ad_has_doc = ad_has_doc + 1 where ad_id = $doc_ad_id";
		$re = q($sq);
		
		$tplUpload_docs->assign("upload_ok",1);
		
	}
}

$lNumUploaded=getNumberOfDocumentsUploaded($doc_ad_id);
$tplUpload_docs->assign("image_upload_size",formatString(LA_UPLOAD_DOC_LIMIT, array($set_doc_limit)));

	
if ($lNumUploaded>=$set_doc_num)
	$lStop=1;	
	
$tplUpload_docs->assign("upload_status", formatString(html_entity_decode(LA_UPLOAD_DOC_STATUS),array("$lNumUploaded","$set_doc_num")));


// If less than maxpic has been uploaded, show upload-form
if (!$lStop)
{
	$tplUpload_docs->assign("show_form",1);
}

if ($doc_ad_id)
{
	$query = "select * from $doc_tbl where f_adid=$doc_ad_id order by f_id desc";
	$sql_result = q ($query);
	$num_pictures =  mysql_num_rows($sql_result);
	
	for ($i=0; $i<$num_pictures; $i++)
	{
	    $row = mysql_fetch_array($sql_result);  
		$id = $row["f_id"];
		$filetype = $row["filetype"];
		$filename = $row["f_file"];
		$lSize=filesize("images/docs/" . $filename);
		$fArray[$i]["size"] = round($lSize/1024);
		
		$lDisplay=ereg_replace($ad_id . "_","",$filename);
		$fArray[$i]["display_name"] = $lDisplay;
		$lExtArray=split("\.",$filename);
		$fArray[$i]["id"] = $id;
		$fArray[$i]["name"] = $filename;
		
		$lExtArray=split("\.",$filename);
		$lExt=end($lExtArray);
		$fArray[$i]["ext"] = $lExt;	
		
	}
	$tplUpload_docs->assign("image_list", $fArray);
}

$tplUpload_docs->assign("id", $doc_ad_id);
$tplUpload_docs->assign("stop", $lStop);
$tplUpload_docs->display("upload_other.tpl");
if (!$admin_area)
{
	include_once("member_footer.php");
	include_once("footer_inc.php"); 
}
?>
